﻿using DataStruct.Sort;

namespace DataStruct
{
    public class Program
    {
        public static void Main(string[] args)
        {
            SelectionSort.Sort();
        }

        //一维数组
        static void ArrayTest()
        {
            Array array = new Array();
            array.InitArray();//数组初始化

            int data = 14;
            //查询一个数据
            int index = array.Get(14);
            if (index == -1)
            {
                Console.WriteLine("没有找到" + data);
            }
            else
            {
                Console.WriteLine("数据" + data + "找到了,下标为" + index);
            }

            Console.Write("插入数据前:");
            array.Print();
            array.Insert(3, 333);
            array.Insert(0, -1);
            array.Insert(1, 111);
            Console.Write("插入数据后:");
            array.Print();

            array.Delete(3);
            Console.Write("删除数据后:");
            array.Print();

            array.Update(0, 123);
            Console.Write("更新数据后:");
            array.Print();
        }

        //单链表
        static void LinkedListTest()
        {
            LinkedList list = new LinkedList();
            list.Print();

            var node = list.GetByIndex(4);
            Console.WriteLine("单链表第4个值:" + node.Data);

            Console.WriteLine("删除头节点");
            list.DeleteByIndex(1);
            list.Print();

            Console.WriteLine("删除尾节点后");
            list.DeleteByIndex(list.GetLength());
            list.Print();

            Console.WriteLine("删除中间第2个节点");
            list.DeleteByIndex(2);
            list.Print();

            Console.WriteLine("1位置插入元素111之后");
            list.Insert(1, 111);
            list.Print();

            Console.WriteLine("最后位置插入元素999之后");
            list.Insert(list.GetLength() + 1, 999);
            list.Print();

            Console.WriteLine("3位置插入元素333之后");
            list.Insert(3, 333);
            list.Print();


            Console.WriteLine("链表拼接后");
            var list1 = new LinkedList();
            list = list.Concatenate(list1);
            list.Print();

            Console.WriteLine("链表反转后");
            list.Reverse();
            list.Print();

            Console.WriteLine("链表长度:" + list.GetLength());
        }

        //循环链表
        static void CircleLinkedListTest()
        {
            CircleLinkedList list = new CircleLinkedList();
            list.Print();

            Console.WriteLine($"第1个节点的值=" + list.GetByIndex(1).Data);
            Console.WriteLine($"第2个节点的值=" + list.GetByIndex(2).Data);
            Console.WriteLine($"第3个节点的值=" + list.GetByIndex(3).Data);
            Console.WriteLine($"第4个节点的值=" + list.GetByIndex(4).Data);

            Console.WriteLine($"第1个节点的值=" + list.GetByData(77)?.Data);
            Console.WriteLine($"第2个节点的值=" + list.GetByData(2)?.Data);
            Console.WriteLine($"第3个节点的值=" + list.GetByData(3)?.Data);
            Console.WriteLine($"第4个节点的值=" + list.GetByData(4)?.Data);

            //Console.WriteLine("删除第一个节点");
            //list.DeleteByIndex(1);
            //list.Print();

            //Console.WriteLine("删除第2个节点");
            //list.DeleteByIndex(2);
            //list.Print();

            //Console.WriteLine("删除第4个节点");
            //list.DeleteByIndex(4);
            //list.Print();

            //Console.WriteLine("删除第一个节点");
            //list.DeleteByValue(5);
            //list.Print();

            //Console.WriteLine("删除最后一个节点");
            //list.DeleteByValue(0);
            //list.Print();

            //Console.WriteLine("删除值为3的节点");
            //list.DeleteByValue(3);
            //list.Print();

            Console.WriteLine("头节点插入111");
            list.Insert(1, 111);
            list.Print();

            Console.WriteLine("尾部插入999");
            list.Insert(list.GetLength() + 1, 999);
            list.Print();

            Console.WriteLine("第三个位置插入333");
            list.Insert(3, 333);
            list.Print();
        }

        //双向链表
        static void DoubleLinkedListTest()
        {
            DoubleLinkedList list = new DoubleLinkedList();
            list.Print();

            list.Insert(1, 111);
            list.Print();

            list.Insert(3, 333);
            list.Print();

            list.Insert(list.GetLength() + 1, 999);
            list.Print();

            Console.WriteLine("第1个节点值:" + list.GetByIndex(1).Data);
            Console.WriteLine("第2个节点值:" + list.GetByIndex(2).Data);
            Console.WriteLine("第3个节点值:" + list.GetByIndex(3).Data);
            Console.WriteLine("第4个节点值:" + list.GetByIndex(4).Data);

            Console.WriteLine("111节点值:" + list.GetByData(111)?.Data);
            Console.WriteLine("2节点值:" + list.GetByData(2)?.Data);
            Console.WriteLine("333节点值:" + list.GetByData(333).Data);
            Console.WriteLine("444节点值:" + list.GetByData(444)?.Data);

            //Console.WriteLine("删除第一个");
            //list.DeleteByIndex(1);
            //list.Print();

            //Console.WriteLine("删除最后一个");
            //list.DeleteByIndex(list.GetLength());
            //list.Print();

            //Console.WriteLine("删除第三个");
            //list.DeleteByIndex(3);
            //list.Print();

            Console.WriteLine("删除值为111的节点");
            list.DeleteByValue(111);
            list.Print();

            Console.WriteLine("删除值为999的节点");
            list.DeleteByValue(999);
            list.Print();

            Console.WriteLine("删除值为333的节点");
            list.DeleteByValue(333);
            list.Print();
        }

        //顺序栈
        static void StackTest()
        {
            Stack stack = new Stack(10);
            for (int i = 0; i < 10; i++)
            {
                stack.Push(i);
            }
            stack.Print();

            Console.WriteLine("堆栈是否满了:" + stack.IsFull());
            stack.Push(10);//kourong 
            stack.Print();

            Console.WriteLine("出栈:" + stack.Pop());
            stack.Print();

            Console.WriteLine("栈顶元素:" + stack.Peek());
            stack.Print();
        }

        //链栈  
        static void LinkStackTest()
        {
            LinkedStack stack = new LinkedStack();
            Console.WriteLine("是否空栈:" + stack.IsEmpty());
            for (int i = 0; i < 10; i++)
            {
                stack.Push(i);
            }
            stack.Print();

            Console.WriteLine("栈顶元素:" + stack.Peek()?.Data);

            Console.WriteLine("出栈:" + stack.Pop()?.Data);
            stack.Print();

            Console.WriteLine("出栈:" + stack.Pop()?.Data);
            stack.Print();

            Console.WriteLine("出栈:" + stack.Pop()?.Data);
            stack.Print();
        }

        //队列-数组实现
        static void QueueTest()
        {
            Queue queue=new Queue();
            for (int i = 0; i < 20; i++)
            {
                queue.EnQueue(i);
            }
            queue.Print();

            for(int i = 0; i < 19; i++)
            {
                Console.WriteLine("出队:"+queue.Dequeue());
                queue.Print();
            }
            Console.WriteLine("出队:" + queue.Dequeue());
        }

        //队列-链表实现
        static void LinkedQueueTest()
        {
            LinkedQueue queue = new LinkedQueue();
            Console.WriteLine("队列是否为空:" + queue.IsEmpty());
            for(int i = 0; i < 10; i++)
            {
                queue.EnQueue(i);
            }
            queue.Print();
            Console.WriteLine("队头:" + queue.Peek()?.Data);

            for (int i = 0; i < 9; i++)
            {
                Console.WriteLine("出队列：" + queue.Dequeue()?.Data);
                queue.Print();
            }

        }

        //循环队列
        static void CircleQueueTest()
        {
            CircleQueue queue = new CircleQueue();
            for(int i = 0; i < 9; i++)
            {
                queue.EnQueue(i);
            }
            queue.Print();

            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
        }

        //循环队列2 count计数法判断队列空和队列满
        static void CircleQueueTest2()
        {
            CircleQueue2 queue = new CircleQueue2();
            for (int i = 0; i < 10; i++)
            {
                queue.EnQueue(i);
            }
            queue.Print();

            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
            Console.WriteLine($"出队:{queue.Dequeue()}");
            queue.Print();
        }
    }
}
